//
//  ZYFirstViewController.m
//  XiangMuYiBai
//
//  Created by mac on 15-4-19.
//  Copyright (c) 2015年 zhiyou. All rights reserved.
//
#define  jianJu  (320 - 5 * 45) / 6.0
#define A  (320 - 55 * 4 ) / 5.0
#import "ZYFirstViewController.h"
#import "Header.h"
@interface ZYFirstViewController ()

@end

@implementation ZYFirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // x1-x4  为0-9  4个按钮tag为11-14
    UIImageView *beiJing = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 320, 480)];
    beiJing.image = [UIImage imageNamed:@"bg@2x.jpg"];
    [self.view addSubview:beiJing];
    NSMutableArray *array=[[NSMutableArray alloc]init];
    for (int i = 0; i < 10; i++)
    {
        
        UIButton *button=[[UIButton alloc]init];
        [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"b_%d.png",i]] forState:UIControlStateNormal];
        button.tag=i+1;
        [array addObject:button];
    }
    int a=arc4random()%10;
    UIButton*bt1=[array objectAtIndex:a];
    x1=bt1.tag-1;
    bt1.tag=11;
    bt1.frame = CGRectMake(A * 1 + 55 * (1 - 1), 150, 55, 55);
    [self.view addSubview:bt1];
    [array removeObjectAtIndex:a];
   int  b=arc4random()%9;
    UIButton*bt2=[array objectAtIndex:b];
    x2=bt2.tag-1;
    bt2.tag=12;
    bt2.frame = CGRectMake(A * 2 + 55 * (2 - 1), 150, 55, 55);
    [self.view addSubview:bt2];
    [array removeObjectAtIndex:b];
    int c=arc4random()%8;
        UIButton*bt3=[array objectAtIndex:c];
        x3=bt3.tag-1;
        bt3.tag=13;
        bt3.frame = CGRectMake(A * 3 + 55 * (3 - 1), 150, 55, 55);
        [self.view addSubview:bt3];
    [array removeObjectAtIndex:c];
    int d=arc4random()%7;
        UIButton*bt4=[array objectAtIndex:d];
        x4=bt4.tag-1;
        bt4.tag=14;
        bt4.frame = CGRectMake(A * 4 + 55 * (4 - 1), 150, 55, 55);
        [self.view addSubview:bt4];
    
    //下面一排按钮的tag 20-29
   NSMutableArray *arrayPan=[[NSMutableArray alloc]init];
   
    for (int i = 1; i < 10; i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        if (i > 5)
        {
            btn.frame=CGRectMake(jianJu * (i -5) + (i - 6) * 45, 380, 40, 40);
            [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"s_%d.png",i]] forState:UIControlStateNormal];
            btn.tag=i + 20;
            [arrayPan addObject:btn];
            
            [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.view addSubview:btn];
        }
        else if(i < 6)
        {
            btn.frame=CGRectMake(jianJu * i + (i - 1) * 45, 300, 40, 40);
            [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"s_%d.png",i]] forState:UIControlStateNormal];
            btn.tag=i + 20;
            [arrayPan addObject:btn];
            
            [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.view addSubview:btn];
            
        }
        

        
    }
    UIButton *bt5=[[UIButton alloc]init];
        [bt5 setBackgroundImage:[UIImage imageNamed:@"s_0.png"] forState:UIControlStateNormal];
        bt5.tag=20;
        [arrayPan addObject:bt5];
         bt5.frame=CGRectMake(jianJu * (10 -5) + (10 - 6) * 45, 380, 40, 40);
        [bt5 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:bt5];
    shangSige=[[NSMutableArray alloc]init];
    [shangSige addObject:bt1];
    [shangSige addObject:bt2];
    [shangSige addObject:bt3];
    [shangSige addObject:bt4];
    

}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    UIButton *btn=(UIButton*)context;
    btn.tag=btn.tag-10;
    btn.bounds=CGRectMake(0, 0, 40, 40);
}

-(void)click:(UIButton*)btn
{
    if (btn.tag>29)
    {
        for (UIButton *bt in shangSige)
        {
            if (CGRectIntersectsRect(btn.frame, bt.frame))
            {
                bt.tag=bt.tag-4;
                break;
            }
        }
        if (btn.tag==30)
        {
            [UIView beginAnimations:nil context:btn];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationRepeatCount:1];
            btn.frame =CGRectMake(jianJu * (10 -5) + (10 - 6) * 45, 380, 40, 40);
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(animationDidStop: finished: context:)];
            [UIView commitAnimations];
        }
       else if (btn.tag<36)
        {
            int i=btn.tag-30;
            [UIView beginAnimations:nil context:btn];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationRepeatCount:1];
            btn.frame =CGRectMake(jianJu * i + (i - 1) * 45, 300, 40, 40);
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(animationDidStop: finished: context:)];
            [UIView commitAnimations];
        }
        else
        {
            int i=btn.tag-30;
            [UIView beginAnimations:nil context:btn];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationRepeatCount:1];
            btn.frame =CGRectMake(jianJu * (i -5) + (i - 6) * 45, 380, 40, 40);
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(animationDidStop: finished: context:)];
            [UIView commitAnimations];
        
        }
    }
    //下面按钮跑上面 tag ＋10  在30-39之间
    //上面4个按钮上有东西 tag＋4 在15-18之间
    UIButton *bt=(UIButton*)[self.view viewWithTag:11];
    if (bt != nil && btn.tag>19 && btn.tag<30)
    {
        btn.bounds=CGRectMake(0, 0, 50, 50);
        if (x1==(btn.tag-20))
        {
            count=count+1;
        }
        btn.tag=btn.tag+10;
        bt.tag=bt.tag+4;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.5];
        [UIView setAnimationRepeatCount:1];
        btn.frame = CGRectMake(A * 1 + 55 * (1 - 1)+2.5, 150+2.5, 50, 50);
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(fanYe)];
        [UIView commitAnimations];
         //[self performSelector:@selector(fanYe) withObject:nil afterDelay:.7];
    }
    UIButton *bt2=(UIButton*)[self.view viewWithTag:12];
    if (bt2 != nil && btn.tag>19 && btn.tag<30)
    {
        btn.bounds=CGRectMake(0, 0, 50, 50);
        if (x2==(btn.tag-20))
        {
            count=count+1;
        }
        btn.tag=btn.tag+10;
        bt2.tag=bt2.tag+4;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.5];
        [UIView setAnimationRepeatCount:1];
        btn.frame = CGRectMake(A * 2 + 55 * (2 - 1)+2.5, 150+2.5, 50, 50);
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(fanYe)];
        [UIView commitAnimations];
         //[self performSelector:@selector(fanYe) withObject:nil afterDelay:.7];
    }
    UIButton *bt3=(UIButton*)[self.view viewWithTag:13];
    if (bt3 != nil && btn.tag>19 && btn.tag<30)
    {
        btn.bounds=CGRectMake(0, 0, 50, 50);
        if (x3==(btn.tag-20))
        {
            count=count+1;
        }
        btn.tag=btn.tag+10;
        bt3.tag=bt3.tag+4;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.5];
        [UIView setAnimationRepeatCount:1];
        btn.frame = CGRectMake(A * 3 + 55 * (3 - 1)+2.5, 150+2.5, 50, 50);
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(fanYe)];
        [UIView commitAnimations];
        // [self performSelector:@selector(fanYe) withObject:nil afterDelay:.7];
    }
    UIButton *bt4=(UIButton*)[self.view viewWithTag:14];
    if (bt4 != nil && btn.tag>19 && btn.tag<30)
    {
        btn.bounds=CGRectMake(0, 0, 50, 50);
        if (x4==(btn.tag-20))
        {
            count=count+1;
        }
        btn.tag=btn.tag+10;
        bt4.tag=bt4.tag+4;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.5];
        [UIView setAnimationRepeatCount:1];
        btn.frame = CGRectMake(A * 4 + 55 * (4 - 1)+2.5, 150+2.5, 50, 50);
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(fanYe)];
        [UIView commitAnimations];
         //[self performSelector:@selector(fanYe) withObject:nil afterDelay:.7];
    }
    
   


}

-(void)fanYe
{
    //正确选择则进入下个页面
    if (count==4)
    {
        ZYAppDelegate *app=[UIApplication sharedApplication].delegate;

        ZYSecondViewController *VC=[[ZYSecondViewController alloc]init];
        UINavigationController *add=[[UINavigationController alloc]initWithRootViewController:VC];
        add.navigationBarHidden=YES;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationRepeatCount:1];
       
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:app.window cache:YES];
        [UIView commitAnimations];
         app.window.rootViewController=add;
    }

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end







